package com.e3.template.model;

import com.e3.web.core.model.SuperModel;
import com.e3.web.widget.model.MyTreeModel;

public abstract class TGFTreeModel extends SuperModel implements MyTreeModel {

	private static final long serialVersionUID = 4045799881411142493L;

	private boolean hasChildren = false;
	
	private Long parentId;

	public boolean hasChildren() {
		return this.hasChildren;
	}

	public void setHasChildren(boolean bool) {
		this.hasChildren = bool;
	}
	
	@SuppressWarnings("unchecked")
	public <X> X getParentModel() {
		return (X)get(getParentFiledName());
	}

	public <X> void setParentModel(X model) {
		set(getParentFiledName(),model);
		if(model!=null){
			setParentId(((SuperModel)model).getId());
			((TGFTreeModel)model).setHasChildren(true);
		}
		else{
			setParentId(null);
		}
	}
	
	public Long getParentId() {
		if(parentId == null){
			return getRefModelId(getParentFiledName());
		}
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	
	public abstract String getParentFiledName();
}
